package com.nsgf.library.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhht.htzx.R;
import com.nsgf.library.ui.adapter.SecondaryCategoryAdapter;

public class StickyHeaderItemDecoration extends RecyclerView.ItemDecoration {
    private final Paint paint;
    private final Rect bounds;
    private final int headerHeight;
    private final int headerBackgroundColor;
    private final int headerTextColor;
    private final float headerTextSize;
    private final float headerTextPadding;

    public StickyHeaderItemDecoration() {
        this.paint = new Paint();
        this.bounds = new Rect();
        this.headerHeight = 120; // 标题高度，单位dp
        this.headerBackgroundColor = 0xFFF5F5F5; // 标题背景色
        this.headerTextColor = 0xFF333333; // 标题文字颜色
        this.headerTextSize = 48; // 标题文字大小，单位sp
        this.headerTextPadding = 36; // 标题文字左右padding，单位dp
    }

    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        // 不在这里绘制，只在onDrawOver中绘制
    }

    @Override
    public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (parent.getLayoutManager() == null) return;

        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
        if (firstVisibleItemPosition == RecyclerView.NO_POSITION) return;

        // 获取当前可见的第一个item
        RecyclerView.ViewHolder firstViewHolder = parent.findViewHolderForAdapterPosition(firstVisibleItemPosition);
        if (firstViewHolder == null) return;

        View firstView = firstViewHolder.itemView;
        int top = 0; // 默认置顶显示

        // 如果下一个item的顶部位置小于headerHeight，说明需要移动header
        if (firstVisibleItemPosition + 1 < parent.getAdapter().getItemCount()) {
            RecyclerView.ViewHolder nextViewHolder = parent.findViewHolderForAdapterPosition(firstVisibleItemPosition + 1);
            if (nextViewHolder != null) {
                View nextView = nextViewHolder.itemView;
                if (nextView.getTop() < headerHeight) {
                    // 计算需要移动的距离，确保平滑过渡
                    int moveDistance = nextView.getTop() - headerHeight;
                    top = Math.max(moveDistance, -headerHeight); // 限制最大移动距离
                }
            }
        }

        // 保存画布状态
        canvas.save();

        // 设置裁剪区域
        canvas.clipRect(0, 0, parent.getWidth(), headerHeight);

        // 绘制悬浮的header
        drawHeader(canvas, parent, firstVisibleItemPosition, top);

        // 恢复画布状态
        canvas.restore();
    }

    private void drawHeader(Canvas canvas, RecyclerView parent, int position, int top) {
        // 设置背景
        paint.setColor(headerBackgroundColor);
        canvas.drawRect(0, top, parent.getWidth(), top + headerHeight, paint);

        // 设置文字
        paint.setColor(headerTextColor);
        paint.setTextSize(headerTextSize);
        paint.setAntiAlias(true);

        // 获取标题文字
        String title = ((SecondaryCategoryAdapter) parent.getAdapter()).getCategoryTitle(position);
        if (title == null) return;

        // 计算文字位置
        paint.getTextBounds(title, 0, title.length(), bounds);
        float x = headerTextPadding;
        float y = top + (headerHeight + bounds.height()) / 2;

        // 绘制文字
        canvas.drawText(title, x, y, paint);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        // 不再添加顶部间距，避免遮挡内容
        outRect.set(0, 0, 0, 0);
    }
} 